/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia.JPAControllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Logica.Core.Depositos.Lote;
import Logica.Core.Producto.YerbaCanchada;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Luciano
 */
public class YerbaCanchadaJpaController implements Serializable {

    public YerbaCanchadaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    
    public YerbaCanchadaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(YerbaCanchada yerbaCanchada) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Lote miLote = yerbaCanchada.getMiLote();
            if (miLote != null) {
                miLote = em.getReference(miLote.getClass(), miLote.getNumero());
                yerbaCanchada.setMiLote(miLote);
            }
            em.persist(yerbaCanchada);
            if (miLote != null) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLote.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLote.setMiProducto(yerbaCanchada);
                miLote = em.merge(miLote);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findYerbaCanchada(yerbaCanchada.getId()) != null) {
                throw new PreexistingEntityException("YerbaCanchada " + yerbaCanchada + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(YerbaCanchada yerbaCanchada) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaCanchada persistentYerbaCanchada = em.find(YerbaCanchada.class, yerbaCanchada.getId());
            Lote miLoteOld = persistentYerbaCanchada.getMiLote();
            Lote miLoteNew = yerbaCanchada.getMiLote();
            if (miLoteNew != null) {
                miLoteNew = em.getReference(miLoteNew.getClass(), miLoteNew.getNumero());
                yerbaCanchada.setMiLote(miLoteNew);
            }
            yerbaCanchada = em.merge(yerbaCanchada);
            if (miLoteOld != null && !miLoteOld.equals(miLoteNew)) {
                miLoteOld.setMiProducto(null);
                miLoteOld = em.merge(miLoteOld);
            }
            if (miLoteNew != null && !miLoteNew.equals(miLoteOld)) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLoteNew.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLoteNew.setMiProducto(yerbaCanchada);
                miLoteNew = em.merge(miLoteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = yerbaCanchada.getId();
                if (findYerbaCanchada(id) == null) {
                    throw new NonexistentEntityException("The yerbaCanchada with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaCanchada yerbaCanchada;
            try {
                yerbaCanchada = em.getReference(YerbaCanchada.class, id);
                yerbaCanchada.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The yerbaCanchada with id " + id + " no longer exists.", enfe);
            }
            Lote miLote = yerbaCanchada.getMiLote();
            if (miLote != null) {
                miLote.setMiProducto(null);
                miLote = em.merge(miLote);
            }
            em.remove(yerbaCanchada);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<YerbaCanchada> findYerbaCanchadaEntities() {
        return findYerbaCanchadaEntities(true, -1, -1);
    }

    public List<YerbaCanchada> findYerbaCanchadaEntities(int maxResults, int firstResult) {
        return findYerbaCanchadaEntities(false, maxResults, firstResult);
    }

    private List<YerbaCanchada> findYerbaCanchadaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from YerbaCanchada as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public YerbaCanchada findYerbaCanchada(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(YerbaCanchada.class, id);
        } finally {
            em.close();
        }
    }

    public int getYerbaCanchadaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from YerbaCanchada as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
